#include "infoin.h"
#include "ui_infoin.h"

infoin::infoin(const QString& ttype,const QString& tnum,const QString& cnum,const QString& snum,const QString& odatetime,QWidget *parent) :
    QDialog(parent),ttype(ttype),tnum(tnum),cnum(cnum),snum(snum),odatetime(odatetime),
    ui(new Ui::infoin)
{
    ui->setupUi(this);
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
}

infoin::~infoin()
{
    delete ui;
}


// 获取当前时间戳
qint64 infoin:: getCurrentTimestamp() {
    QDateTime currentDateTime = QDateTime::currentDateTime();
    return currentDateTime.toSecsSinceEpoch();
}

void infoin::on_confirmButton_clicked()
{
    QString name = ui->nameLineEdit->text();
    QString phone = ui->phoneLineEdit->text();
    QString id = ui->idLineEdit->text();
if(name.isEmpty())
{
    QMessageBox::information(this,"提示","姓名不能为空");
}
else if(phone.isEmpty())
{
    QMessageBox::information(this,"提示","电话不能为空");
}
  else if(id.isEmpty())
  {
      QMessageBox::information(this,"提示","身份证不能为空");
  }
else {
    qint64 timestamp = getCurrentTimestamp();
    QString tstamp=QString::number(timestamp);//获得当前时间戳
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString c_time = currentDateTime.toString("yyyy-MM-dd hh:mm:ss"); //获得当前时间
    //订单号的生成方式为身份证号码后四位+车次号后2位+车厢号+座位号+订票时间时间戳
    QString ordernum=id.right(4)+tnum.right(2)+cnum+snum+tstamp;
    QSqlQuery query;
            query.prepare("insert into 订票单表 values(:ordernum,:id,:name,:phone,:ctime,:tnum,:cnum,:snum,:odatetime,:buyaccount,:ttype)");
            query.bindValue(":name", name);
            query.bindValue(":phone", phone);
            query.bindValue(":id", id);
            query.bindValue(":ctime", c_time);
            query.bindValue(":cnum", cnum);
            query.bindValue(":snum", snum);
            query.bindValue(":odatetime", odatetime);
            query.bindValue(":ordernum", ordernum);
            query.bindValue(":tnum", tnum);
            query.bindValue(":buyaccount", Form::now_account);
            query.bindValue(":ttype",ttype);
            if (query.exec()) {
                // 买票成功

                QSqlQuery query;
                query.prepare("update 余票信息表 set 余票张数=余票张数-1 where 车次号=:train_number and 车票种类=:ttype and 出发时间 =:odatetime");
                query.bindValue(":train_number", tnum);
                query.bindValue(":ttype", ttype);
                query.bindValue(":odatetime", odatetime);
                query.exec();//余票减一
                query.prepare("delete from 退票座位表  where 车厢号=:cnum and 座位号=:snum and 出发时间=:odatetime and 车次号=:train_number; ");
                query.bindValue(":train_number", tnum);
                query.bindValue(":odatetime", odatetime);
                query.bindValue(":cnum", cnum);
                query.bindValue(":snum", snum);
                query.exec();
                QMessageBox::information(this,"提示","购票成功");
                emit sendsignal();this->close();
            } else {
                // 买票失败
                QSqlError error = query.lastError();
                   QString errorMessage = error.text();
                   qDebug() << "Query execution failed:" << errorMessage;
                QMessageBox::information(this,"提示","购票失败");
               qDebug()<<query.lastError().text();

            }
}
}

void infoin::on_cancelButton_clicked()
{
    this->close();
}
